﻿using HardcoreGame.Content.NPCs.Enemies.Events.FrostLegion;
using Microsoft.Xna.Framework;
using System;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Common.GlobalNPCs
{
    public class Snowman144Melting : GlobalNPC
    {
        public override bool InstancePerEntity => true;
        private int meltTimer = 0;
        private const int MeltInterval = 60 * 5;
        public float MeltStage { get; private set; } = 0f;

        public override void AI(NPC npc)
        {
            if (npc.type == NPCID.SnowmanGangsta ||
                npc.type == NPCID.MisterStabby ||
                npc.type == NPCID.SnowBalla ||
                npc.type == ModContent.NPCType<ExplodingSnowman>() ||
                npc.type == ModContent.NPCType<SnowmanHelicopter>() ||
                npc.type == ModContent.NPCType<AssaultSnowman>() ||
                npc.type == ModContent.NPCType<SnowmanGangsta>() ||
                npc.type == ModContent.NPCType<SnowmanTank>() ||
                npc.type == ModContent.NPCType<RPGSnowman>())
            {
                bool inDesert = Main.LocalPlayer.ZoneDesert;

                if (inDesert)
                {
                    meltTimer++;
                    if (meltTimer >= MeltInterval)
                    {
                        meltTimer = 0;
                        MeltStage = Math.Min(1f, MeltStage + 0.2f);
                        SpawnWaterParticles(npc, 30);
                        UpdateStats(npc);
                        if (MeltStage >= 1f)
                        {
                            npc.active = false;
                            SpawnFinalWaterEffect(npc);
                        }
                    }
                    if (Main.rand.NextBool(20))
                    {
                        SpawnWaterParticles(npc, 1);
                    }
                    ApplyDesertSlowdown(npc);
                }
                else
                {
                    MeltStage = Math.Max(0f, MeltStage - 0.001f);
                    meltTimer = 0;
                }
            }
        }

        private void ApplyDesertSlowdown(NPC npc)
        {
            float slowdownFactor = 0.3f * MeltStage;
            npc.velocity.X *= 1f - slowdownFactor;
            if (npc.velocity.Y < 0)
            {
                npc.velocity.Y *= 1f - slowdownFactor * 0.5f;
            }
        }


        private void SpawnWaterParticles(NPC npc, int count)
        {
            for (int i = 0; i < count; i++)
            {
                Vector2 position = new(
                    npc.position.X + Main.rand.NextFloat(npc.width),
                    npc.position.Y + npc.height * (1f - MeltStage * Main.rand.NextFloat(0.8f, 1f)));
                Dust dust = Dust.NewDustPerfect(
                    position,
                    DustID.Water,
                    new Vector2(Main.rand.NextFloat(-1f, 1f), Main.rand.NextFloat(-2f, 0.5f)),
                    100,
                    default,
                    Main.rand.NextFloat(0.8f, 1.5f));

                dust.noGravity = Main.rand.NextBool(3);
            }
        }

        private void UpdateStats(NPC npc)
        {
            npc.defense = (int)(npc.defense * (1f - MeltStage * 0.5f));
            npc.damage = (int)(npc.damage * (1f - MeltStage * 0.3f));
        }

        private static void SpawnFinalWaterEffect(NPC npc)
        {
            for (int i = 0; i < 50; i++)
            {
                Dust.NewDustPerfect(
                    npc.Center,
                    DustID.Water,
                    new Vector2(Main.rand.NextFloat(-3f, 3f), Main.rand.NextFloat(-4f, 1f)),
                    150,
                    default,
                    Main.rand.NextFloat(1f, 2f));
            }
            int waterX = (int)(npc.Center.X / 16);
            int waterY = (int)(npc.Center.Y / 16);

            for (int x = -1; x <= 1; x++)
            {
                for (int y = 0; y <= 1; y++)
                {
                    Tile tile = Main.tile[waterX + x, waterY + y];
                    if (!tile.HasTile && tile.LiquidAmount < 255)
                    {
                        tile.LiquidType = LiquidID.Water;
                        tile.LiquidAmount = 50;
                        WorldGen.SquareTileFrame(waterX + x, waterY + y);
                    }
                }
            }
        }
    }
}